<template>
  <custom-dialog
    ref="dialog"
    :title="modalTitle" height="400px"
    :before-confirm="beforeConfirm" :on-confirm="onConfirm"
  >
    <el-form ref="form" :model="info" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="info.userName" @input="onUserNameChange"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="profilePhoto">
        <custom-upload
          ref="upload"
          :url="info.profilePhoto" :name="info.profilePhotoName"
          @remove="onRemove" @upload="onUpload"
        ></custom-upload>
      </el-form-item>
      <el-form-item label="登录账号" prop="loginName">
        <el-input v-model="info.loginName"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="info.phone"></el-input>
      </el-form-item>
      <el-form-item label="所属企业">
        <el-select v-model="info.tenantId" class="width-100">
          <el-option v-for="item in tenantIdOptions" :key="item.value" v-bind="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="info.email"></el-input>
      </el-form-item>
    </el-form>
  </custom-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { saveOrUpdateUser } from '@/api/sysUser'
import CustomDialog from '@/components/custom-dialog'
import CustomUpload from '@/components/custom-upload'
import { getListEnterprise } from '@/api/enterprose'

const api = { saveOrUpdateUser }
export default {
  name: 'operate-modal',
  props: {
    modalTitle: String,
    data: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  components: {
    CustomDialog,
    CustomUpload
  },
  data () {
    return {
      tenantIdOptions: [],
      info: {
        id: '',
        userName: '',
        phone: '',
        email: '',
        loginName: '',
        profilePhoto: '',
        profilePhotoName: '',
        tenantId: '',
      },
      rules: {
        userName: [{ required: true, message: '请输入姓名' }],
        phone: [{ required: true, message: '请输入手机号' }],
        profilePhoto: [{ required: true, message: '请上传头像' }],
        loginName: [{ required: true, message: '请输入登录账号' }]
      }
    }
  },
  created () {
    this.onUserNameChange = debounce(this.onUserNameChange)
  },
  methods: {
    getOptions () {
      return getListEnterprise().then(res => {
        if (res.code === 200) {
          this.tenantIdOptions = res.data.map(v => ({
            label: v.enterpriseName,
            value: v.id,
          }))
        }
      })
    },
    open () {
      const isAdd = Object.keys(this.data).length === 0
      this.info = isAdd
        ? this.$options.data().info
        : {
            ...this.$options.data().info,
            ...this.data
          }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
      if (this.$refs.upload) {
        this.$refs.upload.clearFiles()
      }
      this.getOptions()
      this.$nextTick(() => {
        this.$refs.dialog.open()
      })
    },
    beforeConfirm () {
      return this.$refs.form.validate()
    },
    onConfirm (close) {
      const params = { ...this.info }
      if (params.id === '') {
        delete params.id
      }
      return api.saveOrUpdateUser(params).then((res) => {
        if (res.code === 200) {
          this.$emit('save-success')
          this.$message.success('保存成功')
          close()
        }
      })
    },
    onRemove () {
      this.info.profilePhoto = ''
      this.info.profilePhotoName = ''
    },
    onUpload (data) {
      this.info.profilePhoto = data.url
      this.info.profilePhotoName = data.urlName
    },
    onUserNameChange (userName) {
      this.info.loginName = this.$pinyin(userName)
    }
  }
}
</script>

<style scoped>

</style>
